/*初始化音视频*/
define(['js/plugins/chat/meeting/adapter', 'js/plugins/chat/meeting/base64.min', 'js/plugins/chat/meeting/MD5.min', 'js/plugins/chat/meeting/ytx-web-im7.2.2.5', 'js/plugins/chat/meeting/static_yoMeeting_ytx-web-av3'], function () {
    function generateTimeNumber() {
        return new Date().format("yyyyMMddHHmmss")
    }

    return {
        loadMeet(initParam, container) {
            const appid = initParam.appId,
                apptoken = initParam.appToken,
                timestamp = generateTimeNumber(),
                userName = initParam.agentName,
                psd = initParam.psd;

            const resp = RL_YTX_NEW.init({appId: appid, serverIp: initParam.serverIp, isV3: true});

            $('.meeting .users', container).html(initParam.userName);

            if (resp.code === 170002) {
                //缺少必要参数，详情见msg参数
                //用户逻辑处理
                $F.alert('缺少必要参数')
            } else if (resp.code === 174001) {
                //不支持HTML5，关闭页面
                //用户逻辑处理
                $F.alert('不支持的H5');
            } else if (resp.code === 200) {
                //初始化成功
                RL_YTX_NEW.chatInit();

                const sig = hex_md5(appid + userName + timestamp + apptoken);
                //账号登录参数设置
                const loginParam = {
                    type: 1, //登录类型 1账号登录，3voip账号密码登录
                    userName: userName, //设置用户名
                    password: psd, //type值为1时，密码可以不赋值
                    sig: sig, //设置sig,
                    timestamp: timestamp, //设置时间戳
                };

                RL_YTX_NEW.login(loginParam, function (obj) {
                    $F.log('Chat - meeting', '账号登录', obj);
                    //音视频无需初始化
                    RL_YTX_NEW.onConnectStateChangeLisenter(function (obj) {
                        //连接状态变更
                        // obj.code;//变更状态 1 断开连接(网络掉线或ws断开) 2 重连中 3 重连成功 4 被踢下线 5 断开连接，需重新登录（超出最大重连次数或重连认证失败）
                        // 断线需要人工重连
                        $F.log('Chat - meeting', '连接状态变更', obj)
                    });

                    RL_Media.onCallMsgListener(function (e) {
                        $F.log('Chat - meeting', 'event', e);
                        if (e.state === 6) {
                            $('.meeting', container).hide();
                            $('.meeting .accept', container).show();
                            if (e.callType === 1) {
                                $('.meeting.videoMeet', container).show();
                                $('.meeting.videoMeet .audio', container).show();
                                $('.meeting.videoMeet .video', container).hide();
                            } else {
                                $('.meeting.audioMeet', container).show();
                                $('.meeting.audioMeet .fir', container).hide();
                                $('.meeting.audioMeet .sec', container).show();
                            }
                        }
                        if (e.state === 5) {
                            $('#audionode', container).hide();
                            $('#selfvideo', container).hide();
                            $('#othervideo', container).hide();
                            $('.meeting', container).hide();
                        }
                        if (e.state === 3) {
                            // audioPlay.pause()
                            RL_Media.setOnTrack(function (e) {
                                $F.log('Chat - meeting', 'setOnTrack', e);
                                if (e.mediaType == 1) {
                                    $('#othervideo', container)[0].srcObject = e.stream;
                                    $('.videoMeet .waitAccept', container).hide();
                                    $('.videoMeet .waitVideo', container).show();
                                    $('#othervideo', container).show()
                                } else {
                                    $('#audionode', container)[0].srcObject = e.stream;
                                    $('#audionode', container).show();
                                    $('.audioMeet .iconBox', container).show();
                                }
                            })
                        }
                    });

                    RL_Media.setOnPublishMedia(function (e) {
                        $F.log('Chat - meeting', 'setOnPublishMedia', e);
                        $('#selfvideo', container).show();
                        $('#selfvideo', container)[0].srcObject = e;
                        $('#audionode', container)[0].srcObject = e;
                    })
                }, function (obj) {
                    //登录失败方法回调
                    $F.err("RL_YTX_NEW.login error: ", obj);
                    if (obj.code === "174002") {
                        $F.alert('和服务器断开连接')
                    }
                });
            }


            // 视频接通
            $('.acceptVCall', container).click(function (e) {
                e.stopPropagation();
                RL_Media.acceptCall(function (obj) {
                    $F.log('Chat - meeting', 'acceptCall', obj);
                    $('.meeting.videoMeet .audio', container).hide();
                    $('.meeting.videoMeet .video', container).show();
                }, function (err) {
                    $F.log('Chat - meeting', 'acceptCallError', err)
                })
            });
            //视频挂断
            $('.releaseVCall', container).click(function (e) {
                e.stopPropagation();
                RL_Media.releaseCall(function (obj) {
                    $F.log('Chat - meeting', 'releaseCall', obj);
                    $('#selfvideo', container).hide();
                    $('#othervideo', container).hide();
                    $('.meeting', container).hide();
                    // $('#audionode').hide()
                }, function (err) {
                    $F.log('Chat - meeting', 'releaseCallError', err)
                })
            });
            // 视频拒接
            $('.rejectVCall', container).click(function (e) {
                e.stopPropagation();
                RL_Media.rejectCall(function (obj) {
                    $F.log('Chat - meeting', 'rejectCall', obj);
                    $('.meeting', container).hide()
                    // audioPlay.pause()
                }, function (err) {
                    $F.log('Chat - meeting', 'rejectCallError', err)
                })
            });
            // 音频接通
            $('.acceptACall', container).click(function (e) {
                e.stopPropagation();
                RL_Media.acceptCall(function (obj) {
                    $F.log('Chat - meeting', 'acceptCall', obj);
                    $('.meeting.audioMeet .fir', container).show();
                    $('.meeting.audioMeet .sec', container).hide();
                    $('.meeting .accept', container).hide()
                    // audioPlay.pause()
                    // $('#audionode').show()
                }, function (err) {
                    $F.log('Chat - meeting', 'acceptCallError', err)
                })
            });
            //音频挂断
            $('.releaseACall', container).click(function (e) {
                e.stopPropagation();
                RL_Media.releaseCall(function (obj) {
                    $F.log('Chat - meeting', 'releaseCall', obj);
                    $('.meeting', container).hide()
                    // $('#audionode').hide()
                }, function (err) {
                    $F.log('Chat - meeting', 'releaseCallError', err)
                })
            });
            // 音频拒接
            $('.rejectACall', container).click(function (e) {
                e.stopPropagation();
                RL_Media.rejectCall(function (obj) {
                    $F.log('Chat - meeting', 'rejectCall', obj);
                    $('.meeting', container).hide();
                    // audioPlay.pause()
                }, function (err) {
                    $F.log('Chat - meeting', 'rejectCallError', err)
                })
            })
        }
    };
});